const { queryLableByName, create } = require("../service/label.service");

const verifyLabelExists = async (ctx, next) => {
  // 先拿到所有的labels
  const { labels } = ctx.request.body;

  // 验证里面有雾label 有 后去id 没有加入数据库拿到id

  const newLabels = [];
  for (let name of labels) {
    const result = await queryLableByName(name);
    const labelObj = { name };
    if (result) {
      //
      labelObj.id = result.id;
    } else {
      // 创建一个标签
      const [result] = await create(name);
      labelObj.id = result.insertId;
    }

    newLabels.push(labelObj);
  }
  ctx.labels = newLabels;
  await next();
};

module.exports = {
  verifyLabelExists,
};
